package com.movie.demo.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.movie.demo.Utlis.token.Tokenobj;
import com.movie.demo.pojo.User;
import com.movie.demo.Utlis.result.CommonResult;
import com.movie.demo.Utlis.token.TokenUtlis;
import com.movie.demo.service.impl.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;

@RestController
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/Login")
    public CommonResult<Object> login(@RequestBody  User users) {
        User user = userService.login(users.getAccount(), users.getPassword());
        if(user!=null)
        {
            HashMap<String,Object> Tuser =Tokenobj.tokenobj(user);
            return CommonResult.success(Tuser);
        }
        return CommonResult.failed();
    }
    @PostMapping("/Register")
    public CommonResult<Object> register(@RequestBody  User user){
        Boolean flag=userService.register(user);
        if (flag) {
            HashMap<String,Object> Tuser =Tokenobj.tokenobj(user);
            return CommonResult.success(Tuser);
        }
        return CommonResult.failed();
    }
    @PostMapping("/Select")
    public  CommonResult<List> select(){
        List<User> userList = userService.select();
        if(userList!=null){
            return CommonResult.success(userList);
        }
        return CommonResult.failed();
    }
    @PostMapping("/Update")
    public CommonResult Update(@RequestBody @Valid User user){
        boolean result=userService.updateUser(user);
        if (result) {
            return CommonResult.success(result);
        }
        return CommonResult.failed();
    }
    @PostMapping("/Delete")
    public CommonResult Delete( @RequestBody  User UserID){
        boolean result=userService.deleteUser(UserID.getUid());
        if (result) {
            return CommonResult.success(result);
        }
        return CommonResult.failed();
    }
    @RequestMapping("/SearchUser")
    public  CommonResult<List> serachuser(@NotNull @RequestParam("usersearch") String usersearch){
        List<User> userList = userService.serachuser(usersearch);
        return CommonResult.success(userList);
    }
}
